﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using DTO;
using BUS;


namespace TCV_WEB.Admin.Page
{
    public partial class Productpage : System.Web.UI.Page
    {
        private int iNewcatid = -1;
        private String sErr = "";
        private String _sErr = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["IDCategory"] != null)
                {
                    hdCate.Value = Session["IDCategory"].ToString();
                    Session["IDCategory"] = null;
                }
            }
        }


        private void LoadDataGrid()
        {
            DataTable dtList = new DataTable();
            try
            {
                ProductsControl prd_control = new ProductsControl();
                dtList = prd_control.GetAll(ref _sErr);

                RadGrid1.DataSource = dtList;
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
            finally
            {
                dtList.Dispose();
            }
        }

        protected void LoadDataGrid1(int iID)
        {
            DataTable dtList = new DataTable();
            try
            {
                DataTable dtb;
                ProductsControl prd_control = new ProductsControl();

                CategoriesControl ct = new CategoriesControl();
                CategoriesInfo cinfo = ct.Get(iID, ref _sErr);
                DataTable temp = ct.GetAll_CategoriesChilds(cinfo, ref _sErr);

                dtb = prd_control.get_allproduct(temp, ref _sErr);
                RadGrid1.DataSource = dtb;
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
            finally
            {
                dtList.Dispose();
            }
        }

        protected void btAddnew_Click(object sender, EventArgs e)
        {
            Session["productid"] = null;
            Response.Redirect("~/Admin/Page/DetailProduct.aspx");
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (hdCate.Value != "")
            {
                int iNewcatid = Convert.ToInt32(hdCate.Value);
                LoadDataGrid1(iNewcatid);
            }
            else
            {
                LoadDataGrid();
            }
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "DELETE")
            {
                try
                {
                    int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                    ProductsControl prd_control = new ProductsControl();

                    lblErr.Text = prd_control.Delete(inewsid);
                    RadGrid1.Rebind();
                }
                catch (System.Exception ex)
                {
                    lblErr.Text = ex.Message;
                }
            }
            if (e.CommandName.ToUpper() == "LOAD")
            {
                int inewsid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                Session["productid"] = inewsid;
                Response.Redirect("~/Admin/Page/DetailProduct.aspx");
            }
        }


    }
}
